package com.smedi.ismedi.inmail.core.port.adapter.service.impl;

import com.smedi.ismedi.inmail.core.port.adapter.service.WeatherService;
import com.smedi.ismedi.inmail.core.port.adapter.service.dto.WeatherHistoryResponse;
import com.smedi.ismedi.inmail.core.port.adapter.service.dto.WeatherNowResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@RequiredArgsConstructor
@Service
public class WeatherServiceImpl implements WeatherService {
    @Value("${weather.url.history}")
    private String weatherUrlHistory;
    @Value("${weather.url.now}")
    private String weatherUrlNow;
    @Value("${q.key}")
    private String qKey;


    private final RestTemplateBuilder restTemplateBuilder;

    @Override
    public WeatherHistoryResponse getWeatherHistory(Long locationId, LocalDate date, String key) {
        RestTemplate restTemplate = restTemplateBuilder.build();
        int code = 0;
        WeatherHistoryResponse weatherHistoryResponse;
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String dateFormat = date.format(dateTimeFormatter);

        // 发送请求
        HttpHeaders headers = new HttpHeaders();

        HttpMethod method = HttpMethod.GET;

        Map<String, String> uriVariables = new HashMap<>();
        uriVariables.put("location", locationId.toString());
        uriVariables.put("date", dateFormat);
        uriVariables.put("key", qKey);

        HttpEntity<Object> requestEntity = new HttpEntity<>(headers);
        Optional<ResponseEntity<WeatherHistoryResponse>> response = Optional.of(restTemplate.exchange(weatherUrlHistory, method, requestEntity, WeatherHistoryResponse.class, uriVariables));

        Optional<Integer> optionalCode = response.map(HttpEntity::getBody).map(WeatherHistoryResponse::getCode);
        if (!optionalCode.isPresent()) {
            return new WeatherHistoryResponse();
        } else {
            code = optionalCode.get();
        }

        if (code != 200) {
            return new WeatherHistoryResponse();
        }

        Optional<WeatherHistoryResponse> optionalWeatherResponse = response.map(HttpEntity::getBody);
        if (!optionalWeatherResponse.isPresent()) {
            return new WeatherHistoryResponse();
        } else {
            weatherHistoryResponse = optionalWeatherResponse.get();
        }

        return weatherHistoryResponse;
    }

    @Override
    public WeatherNowResponse getWeatherNow(Long locationId, String key) {
        RestTemplate restTemplate = restTemplateBuilder.build();
        int code = 0;
        WeatherNowResponse weatherHourly;

        // 发送请求
        HttpHeaders headers = new HttpHeaders();

        HttpMethod method = HttpMethod.GET;

        Map<String, String> uriVariables = new HashMap<>();
        uriVariables.put("location", locationId.toString());
        uriVariables.put("key", qKey);

        HttpEntity<Object> requestEntity = new HttpEntity<>(headers);
        Optional<ResponseEntity<WeatherNowResponse>> response = Optional.of(restTemplate.exchange(weatherUrlNow, method, requestEntity, WeatherNowResponse.class, uriVariables));

        Optional<Integer> optionalCode = response.map(HttpEntity::getBody).map(WeatherNowResponse::getCode);
        if (!optionalCode.isPresent()) {
            return new WeatherNowResponse();
        } else {
            code = optionalCode.get();
        }

        if (code != 200) {
            return new WeatherNowResponse();
        }

        Optional<WeatherNowResponse> optionalWeatherNowResponse = response.map(HttpEntity::getBody);
        if (!optionalWeatherNowResponse.isPresent()) {
            return new WeatherNowResponse();
        } else {
            weatherHourly = optionalWeatherNowResponse.get();
        }

        return weatherHourly;
    }
}
